﻿package field{
	
	import fl.motion.ColorMatrix;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	/*Class for working with balls. Instances of 
	 * this class must be created in GamePanel.as
	 * This class is responsible for interaction balls and mouse*/
	
	public class Ball extends Element {
				
		static public const RED = 0xFF0000;
		static public const GREEN = 0x00FF00;
		static public const BLUE = 0x0000FF;
		
		public function get color(): Number {
			return _color;
		}
		
		public function set color(n: Number): void {
			_color = n;
			filters = [new ColorMatrixFilter(getColorMatrix(n))];
		}
		private var _color: Number = 0;
		
		
		public var colors: Array = [0xFF0000, 0x00FF00, 0x0000FF, 0x00FFFF, 0xFFFF00, 0xFF00FF];
		
		public function getNewColor(): Number {
			return colors[ Math.min( Math.floor(Math.random() * (main.level.level + 2) ), (colors.length - 1) )];
		}
										
		public function getColorMatrix(color: Number): Array {
			var r: Number = ((color & RED) >> 16) / 255;
			var g: Number = ((color & GREEN) >> 8) / 255;
			var b: Number = (color & BLUE) / 255;
			
			return [ r, 0, 0, 0, 0,
					 0, g, 0, 0, 0,
					 0, 0, b, 0, 0,
					 0, 0, 0, 1, 0];
		}
		
		public override function onAddedToStage(e: Event) {	
			super.onAddedToStage(e);
			if(currentLabel != "nextLevel")
				color = getNewColor();
			else
				color = colors[Math.min(main.level.level + 2, colors.length - 1)];
		}
		
		public function Ball() {
			// constructor code
			//addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			//addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			
			filters = [];
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			enableMouseControl();
		}
		
		public override function onMouseOver(e: MouseEvent): void {
			if (main.currentBonus == null){
				panel.findElementsUnderCursor(tileX, tileY, this.color);
				panel.blinkElementsUnderCursor();
			}else {
				main.currentBonus.select(tileX, tileY);
			}
		}

	}
}
